package com.graduation.system.controller;

import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import cn.circle.commom.controller.BackendBaseController;
import com.graduation.system.model.SysNotice;
import com.graduation.system.model.SysDepartment;
import cn.circle.system.util.ReturnInfo;
import cn.circle.system.service.ISysNoticeService;
import cn.circle.system.service.ISysDepartmentService;
import cn.circle.system.util.DataGrid;
import cn.circle.system.util.PageHelper;
import cn.circle.system.util.MD5;

@Controller
@EnableWebMvc
@RequestMapping("/manage/sysNotice")
public class SysNoticeController extends BackendBaseController{

	private static final Log log = LogFactory.getLog(SysNoticeController.class);
	
	@Autowired
	private ISysNoticeService sysNoticeService;
	@Autowired
	private ISysDepartmentService sysDepaermentService;
	


	
	/**
	 * 跳转到customer首页
	 */
	@RequestMapping(value = "/index")
	public String sysCustomerIndex(Map<String, Object> map) {
		List<SysDepartment> dep = this.getDepartment().getRows();
		map.put("dep", dep);
		return "/WEB-INF/jsp/system/system/notice/sys_notice_list.jsp";
	}
	
	/**
	 * 查询客户信息
	 */
	@ResponseBody
	@RequestMapping(value = "/dataGrid")
	public DataGrid dataGrid(@ModelAttribute SysNotice c, String noticeTitle, String noticeSchool, PageHelper ph,HttpServletRequest request) {
		ph.setSort("noticeID");
		ph.setOrder("desc");
		DataGrid dg = sysNoticeService.datagrid(ph,c,noticeSchool,noticeTitle);
		return dg;
	}
	
	/**
	 * 跳转至 syscustomer_au页(添加和修改共用一个编辑页)
	 */
	@RequestMapping(value = "/add")
	public String add(Map<String, Object> map) {
		log.info("----Ok----");
		List<SysDepartment> dep = this.getDepartment().getRows();
		map.put("saveUrl", "/manage/sysNotice/addSave");
		map.put("dep", dep);
		return "/WEB-INF/jsp/system/system/notice/sys_notice_au.jsp";
	}

	
	/**
	 * 保存添加数据
	 */
	@ResponseBody
	@RequestMapping(value = "/addSave")
	public ReturnInfo addSave(@Valid SysNotice sysNotice, BindingResult b, ReturnInfo result,
			HttpServletRequest request) {
		if (b.getErrorCount() > 0) { // 数据校验
			for (FieldError error : b.getFieldErrors()) {
				log.info(error.getField() + ":" + error.getDefaultMessage());
				result.setMsg(error.getDefaultMessage());
			}
			result.setMsg("保存失败！");
			result.setSuccess(1);
		} else {
			if (sysNotice != null) {
				try {
					sysNoticeService.save(sysNotice);
				} catch (Exception e) {
					log.info("保存失败" + e.getMessage());
					result.setMsg("保存失败！");
					result.setSuccess(1);
				}
			}
		}
		return result;
	}

	/**
	 * 获得公共详细资料
	 */
	@RequestMapping(value = "/queryCustDetail")
	public String queryCustDetail(String id, Map<String, Object> map) {
		log.info(id);
		if (id != null) {
			List<SysNotice> model = sysNoticeService.getUser(id);
			map.put("model", model.get(0));
		}
		return "/WEB-INF/jsp/system/system/notice/sys_notice_detail.jsp";
	}
	/**
	 * 删除若干条公告
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public ReturnInfo delete(String custIds, ReturnInfo result) {
		if(sysNoticeService.delete(custIds)){
			result.setMsg("删除成功！");
			result.setSuccess(0);
		}else{
			result.setMsg("删除失败！");
			result.setSuccess(1);
		}
		return result;
	}
	/**
	 * 获取部门信息
	 */
	public DataGrid getDepartment(){
		return sysDepaermentService.datagrid();
	}
}
